package com.aisb.manage;

import com.aisb.core.Result;
import com.aisb.core.log.LogOper;
import com.aisb.core.log.LogVisit;
import com.aisb.core.util.DateUtil;
import com.aisb.core.util.file.ExcelUtils;
import com.github.pagehelper.Page;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

@Controller
@RequestMapping("/manage/role")
public class RoleController {
    private static Logger logger = LoggerFactory.getLogger(RoleController.class);

    @Autowired
    RoleService roleService;

    @Autowired
    private AuthService authService;

    @RequestMapping(value = "/index")
    @LogVisit("角色管理列表")
    public String index(Model model) {
        return "manage/role";
    }

    @RequestMapping(value = "/add/index")
    public String indexAdd(Model model) {
        return "manage/role_add";
    }

    @RequestMapping(value = "/view/{roleId}")
    public String indexView(Model model, @PathVariable String roleId) {
        Object o = roleService.roleOne(roleId);
        model.addAttribute("roleInfo", o);

        return "manage/role_view";
    }

    @RequestMapping(value = "/modify/{roleId}")
    public String indexModify(Model model, @PathVariable String roleId) {
        Object o = roleService.roleOne(roleId);
        model.addAttribute("roleInfo", o);

        return "manage/role_modify";
    }

    @RequestMapping(value = "/list")
    @ResponseBody
    public Result list(@RequestParam Map param) {
        String pageNumStr = (String) param.get("pageNum");
        String pageSizeStr = (String) param.get("pageSize");

        Integer pageNum = Integer.parseInt(pageNumStr);
        Integer pageSize = Integer.parseInt(pageSizeStr);

        Page page = roleService.roleList(param, pageNum, pageSize);

        Map r = new HashMap();
        r.put("total", page.getTotal());
        r.put("rows", page.getResult());

        return new Result(true, r);
    }

    @RequestMapping(value = "/add")
    @ResponseBody
    public Result add(@RequestParam Map param) {
        String funcs = (String) param.get("funcs");

        List<String> funcIds = new ArrayList<String>();

        if (null != funcs && funcs.length() > 0) {
            String[] u = funcs.split(";");
            funcIds = Arrays.asList(u);
        }

        int result = roleService.roleAdd(param, funcIds);

        return new Result(true, result);
    }

    @RequestMapping(value = "/checkexist")
    @ResponseBody
    public Map checkexist(@RequestParam Map param) {
        Map result = new HashMap();

        Object o = roleService.roleOne(param);
        if (null == o) {
            result.put("valid", true);
        } else {
            result.put("valid", false);
        }

        return result;
    }

    @RequestMapping(value = "/modify")
    @ResponseBody
    @LogOper("角色修改")
    public Result modify(@RequestParam Map param) {
        int result = roleService.roleModify(param);

        return new Result(true, result);
    }

    @RequestMapping(value = "/delete")
    @ResponseBody
    @LogOper("角色删除")
    public Result delete(@RequestParam Map param) {
        int result = roleService.roleDelete(param);

        return new Result(true, result);
    }

    @RequestMapping(value = "/export")
    @RequiresPermissions("/manage/role/download")
    @ResponseBody
    @LogOper("角色管理，数据导出")
    public Result export(@RequestParam Map param) {
        List<Map> rList = roleService.roleList(param);

        String fileName = "角色_" + DateUtil.getInstance().getToday("yyyyMMddHmmsss") + ".xls";
        boolean flag = ExcelUtils.getInstance().createExcel(fileName, rList);

        return new Result(flag, fileName);
    }

    @RequestMapping(value = "/save/rolefunc")
    @ResponseBody
    @LogOper("角色管理，功能配置")
    public Result saveRoleFunc(@RequestParam Map param) {
        String funcs = (String) param.get("funcs");
        String roles = (String) param.get("roles");

        List<String> funcIds = new ArrayList<String>();
        List<String> roleIds = new ArrayList<String>();


        if (null != funcs && funcs.length() > 0) {
            String[] u = funcs.split(";");
            funcIds = Arrays.asList(u);
        }
        if (null != roles && roles.length() > 0) {
            String[] j = roles.split(";");
            roleIds = Arrays.asList(j);
        }

        int result = authService.authAddRoleFunc(roleIds, funcIds, true);

        return new Result(true, result);
    }

    @RequestMapping(value = "/funcs/{roleId}")
    @ResponseBody
    public Result funcsRoleId(@RequestParam Map param, @PathVariable String roleId) {
        param.put("roleId", roleId);

        List<Map> fList = authService.funcList();
        List<Map> rfList = authService.funcsForRole(param);

        Map<String, Map> raMap = new HashMap();
        if (null != rfList) {
            for (Map m : rfList) {
                String funcId = (String) ((Map) m).get("funcId");
                String funcName = (String) ((Map) m).get("funcName");
                raMap.put(funcId, m);
            }
        }

        for (Map m : fList) {
            String funcId = (String) ((Map) m).get("id");
            if (raMap.containsKey(funcId)) {
                Map rfMap = raMap.get(funcId);
                m.put("checked", true);
                m.putAll(rfMap);
            }
        }

        Map r = new HashMap();
        r.put("funcTotalNum", fList.size());
        r.put("funcs", fList);
        r.put("funcSelNum", raMap.size());

        return new Result(true, r);
    }

    @RequestMapping(value = "/funcs")
    @ResponseBody
    public Result funcs(@RequestParam Map param) {
        List<Map> fList = authService.funcList();

        Map r = new HashMap();
        r.put("funcTotalNum", fList.size());
        r.put("funcs", fList);

        return new Result(true, r);
    }
}
